import json

from flask import Flask, request, jsonify
from . import scale_api
from scales import GAD_7, PHQ_9, PRMQ, PSQI, PSS_10, PSWQ, QLQ_C30, RRS,SCL_90
from server import sqlscaleserver
app = Flask(__name__)

@scale_api.route('/calculate_gad7', methods=['POST'])
def gad7_endpoint():
    data = request.json
    answers = data.get('answers', {})

    if not answers or len(answers) != 7:
        return jsonify({'error': 'Invalid input. Please provide answers for all 7 questions.'}), 400

    gad7 = GAD_7.GAD7(answers)
    gad7.calculate_score()
    total_score, severity = gad7.get_scores()
    result = json.dumps({'total_score': total_score, 'severity': severity})
    # 添加到数据库
    scale_name = 'gad_score'
    scale_answers="gad_jilu"
    answers=json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'), scale_name, result,scale_answers,answers)

    return result

"""
客户端发送请求数据
{   
    "user_id": "1",
    "answers": {
        "q1": "3",
        "q2": "2",
        "q3": "1",
        "q4": "0",
        "q5": "2",
        "q6": "1",
        "q7": "3"
    }
}
服务器返回计算的总分和抑郁严重程度：
{
    "total_score": 12,
    "severity": "中度焦虑"
}
"""

@scale_api.route('/calculate_phq9', methods=['POST'])
def calculate_phq9():
    data = request.json
    answers = data.get('answers', {})

    if not answers or len(answers) != 9:
        return jsonify({'error': 'Invalid input'}), 400

    phq9 = PHQ_9.PHQ9(answers)
    phq9.calculate_score()
    total_score, severity = phq9.get_scores()
    result = json.dumps({'total_score': total_score, 'severity': severity})
    # 添加到数据库
    scale_name = 'phq_score'
    scale_answers = "phq_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'),  scale_name, result,scale_answers,answers)

    return result
"""
发送请求数据
{   
    "user_id": "1",
    "answers": {
        "q1": "3",
        "q2": "2",
        "q3": "1",
        "q4": "0",
        "q5": "2",
        "q6": "1",
        "q7": "3",
        "q8": "2",
        "q9": "1"
    }
}
服务器将返回计算的总分和抑郁严重程度：
{
    "total_score": 15,
    "severity": "中重度抑郁"
}
"""


@scale_api.route('/calculate_prmq', methods=['POST'])
def calculate_prmq():
    data = request.json
    answers = data.get('answers', {})

    if not answers or len(answers) != 16:
        return jsonify({'error': 'Invalid input'}), 400

    prmq = PRMQ.PRMQ(answers)
    prmq.calculate_scores()
    scores = json.dumps(prmq.get_scores())

    scale_name = 'prmq_score'
    scale_answers = "prmq_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'), scale_name, scores,scale_answers,answers)

    return scores
"""
发送请求数据
{   
    "user_id": "1",
    "answers": {
        "q1": "3", "q2": "2", "q3": "1", "q4": "5", "q5": "2", "q6": "1", "q7": "3", "q8": "2",
        "q9": "1", "q10": "5", "q11": "3", "q12": "2", "q13": "1", "q14": "5", "q15": "2", "q16": "1"
    }
}
服务器将返回计算的总分、前瞻性记忆得分和回顾性记忆得分：
{
    "total_score": 39,
    "prospective_memory_score": 19,
    "retrospective_memory_score": 20
}
"""


@scale_api.route('/calculate_psqi', methods=['POST'])
def calculate_psqi_api():
    data = request.json
    print(data)
    answers = data.get('answers', {})

    if not answers or len(answers) != 18:
        return jsonify({'error': 'Invalid input'}), 400

    result = json.dumps(PSQI.calculate_psqi(answers))
    print(result)
    scale_name = 'psqi_score'
    scale_answers = "psqi_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'),scale_name, result,scale_answers,answers)

    return result

"""
客户端发送请求体格式：
{   
    "user_id": "1",
    "answers": {
        "q1": 22,
        "q2": 1,
        "q3": 10,
        "q4": 8,
        "q5a": 0,
        "q5b": 1,
        "q5c": 2,
        "q5d": 3,
        "q5e": 0,
        "q5f": 1,
        "q5g": 2,
        "q5h": 3,
        "q5i": 0,
        "q5j": 1,
        "q6": 2,
        "q7": 1,
        "q8": 2,
        "q9": 3
    }
}
服务器将返回计算的组件得分、总分和睡眠质量评估：
{
    "total_score": 18,
    "sleep_quality": "差",
    "component_scores": {
        "A": 2,
        "B": 1,
        "C": 0,
        "D": 3,
        "E": 3,
        "F": 1,
        "G": 2
    }
}
"""

@scale_api.route('/calculate_pss10', methods=['POST'])
def calculate_pss10():
    data = request.json
    answers = data.get('answers', {})

    if not answers or len(answers) != 10:
        return jsonify({'error': 'Invalid input'}), 400

    pss10 = PSS_10.PSS10(answers)
    pss10.calculate_score()
    total_score, stress_level = pss10.get_scores()
    result = json.dumps({'total_score': total_score, 'stress_level': stress_level})
    scale_name = 'pss_score'
    scale_answers = "pss_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'), scale_name, result,scale_answers,answers)

    return result
"""
请求体格式
{
    "user_id": "1",
    "answers": {
        "q1": "4",
        "q2": "2",
        "q3": "1",
        "q4": "0",
        "q5": "2",
        "q6": "1",
        "q7": "3",
        "q8": "2",
        "q9": "1",
        "q10": "0"
    }
}
返回数据：
{
    "total_score": 15,
    "stress_level": "压力较低或者中等，应对能力尚可"
}
"""


@scale_api.route('/calculate_pswq', methods=['POST'])
def calculate_pswq():
    data = request.json
    answers = data.get('answers', {})

    if not answers or len(answers) != 16:
        return jsonify({'error': 'Invalid input'}), 400

    pswq = PSWQ.PSWQ(answers)
    pswq.calculate_score()
    total_score, worry_level = pswq.get_scores()
    result = json.dumps({'total_score': total_score, 'worry_level': worry_level})
    scale_name = 'pswq_score'
    scale_answers = "pswq_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'), scale_name, result,scale_answers,answers)

    return result

"""
请求体
{   
    "user_id": "1",
    "answers": {
        "q1": "3",
        "q2": "2",
        "q3": "1",
        "q4": "5",
        "q5": "2",
        "q6": "1",
        "q7": "3",
        "q8": "2",
        "q9": "1",
        "q10": "5",
        "q11": "3",
        "q12": "2",
        "q13": "1",
        "q14": "5",
        "q15": "2",
        "q16": "1"
    }
}
服务器将返回计算的总分和担忧水平：
{
    "total_score": 40,
    "worry_level": "中等担忧水平"
}
"""


@scale_api.route('/calculate_qlq_c30', methods=['POST'])
def calculate_qlq_c30():
    data = request.json
    answers = data.get('answers', {})

    if not answers or len(answers) != 30:
        return jsonify({'error': 'Invalid input'}), 400

    qlq_c30 = QLQ_C30.QLQ_C30(answers)
    qlq_c30.calculate_scores()
    domain_scores, standard_scores = qlq_c30.get_scores()
    # 将 domain_scores 和 standard_scores 合并成一个字典
    combined_scores = {
        "domain_scores": domain_scores,
        "standard_scores": standard_scores
    }
    # 将合并后的字典转换为 JSON 字符串
    json_score = json.dumps(combined_scores)
    print(f'json_score:{json_score}')
    scale_name = 'qlq_score'
    scale_answers = "qlq_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'), scale_name, json_score,scale_answers,answers)

    return jsonify({
        'domain_scores': domain_scores,
        'standard_scores': standard_scores
    })

"""
客户端请求体：
{
    "user_id": "1",
    "answers": {
        "q1": "3", "q2": "2", "q3": "1", "q4": "4", "q5": "3", "q6": "2", "q7": "1", "q8": "4",
        "q9": "3", "q10": "2", "q11": "1", "q12": "4", "q13": "3", "q14": "2", "q15": "1", "q16": "4",
        "q17": "3", "q18": "2", "q19": "1", "q20": "4", "q21": "3", "q22": "2", "q23": "1", "q24": "4",
        "q25": "3", "q26": "2", "q27": "1", "q28": "4", "q29": "3", "q30": "2"
    }
}
服务器将返回计算的领域得分和标准化得分：
{
    "domain_scores": {
        "physical_functioning": 2.6,
        "role_functioning": 1.5,
        "emotional_functioning": 3.5,
        "cognitive_functioning": 4.0,
        "social_functioning": 1.5,
        "global_health_status": 2.5,
        "fatigue": 2.0,
        "nausea_vomiting": 1.5,
        "pain": 2.0,
        "dyspnea": 4.0,
        "insomnia": 1.0,
        "appetite_loss": 3.0,
        "constipation": 4.0,
        "diarrhea": 3.0,
        "financial_difficulties": 4.0
    },
    "standard_scores": {
        "physical_functioning": 86.66666666666667,
        "role_functioning": 83.33333333333334,
        "emotional_functioning": 87.5,
        "cognitive_functioning": 100.0,
        "social_functioning": 83.33333333333334,
        "global_health_status": 91.66666666666666,
        "fatigue": 50.0,
        "nausea_vomiting": 66.66666666666666,
        "pain": 50.0,
        "dyspnea": 100.0,
        "insomnia": 0.0,
        "appetite_loss": 66.66666666666666,
        "constipation": 100.0,
        "diarrhea": 66.66666666666666,
        "financial_difficulties": 100.0
    }
}

"""

@scale_api.route('/calculate_rrs', methods=['POST'])
def calculate_rrs():
    data = request.json
    answers = data.get('answers', {})

    if not answers or len(answers) != 22:
        return jsonify({'error': 'Invalid input'}), 400

    rrs = RRS.RRS(answers)
    rrs.calculate_scores()
    scores = json.dumps(rrs.get_scores())

    scale_name = 'rrs_score'
    scale_answers = "rrs_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'), scale_name, scores,scale_answers,answers)

    return scores

"""
请求体：
{   
    "user_id": "1",
    "answers": {
        "q1": "3", "q2": "2", "q3": "1", "q4": "0", "q5": "2", "q6": "1", "q7": "3", "q8": "2", "q9": "1", "q10": "0",
        "q11": "3", "q12": "2", "q13": "1", "q14": "0", "q15": "2", "q16": "1", "q17": "3", "q18": "2", "q19": "1", "q20": "0",
        "q21": "3", "q22": "2"
    }
}
服务器将返回计算的总分和各维度得分：
{
    "total_score": 44,
    "symptom_rumination_score": 22,
    "intrusive_rumination_score": 11,
    "reflective_pondering_score": 11
}
"""
@scale_api.route('/scl_90', methods=['POST'])
def scl_90():
    data = request.json
    answers = data.get('answers', {})
    print(len(answers))
    if not answers or len(answers) != 90:
        return jsonify({'error': 'Invalid input'}), 400

    scl90 = SCL_90.SCL_C90(answers)

    scores = json.dumps(scl90.get_scores())

    scale_name = 'scl_score'
    scale_answers = "scl_jilu"
    answers = json.dumps(answers)
    sqlscaleserver.add_psychological_score(data.get('user_id'), data.get('idd'), scale_name, scores,scale_answers,answers)

    return scores




